package com.zcaxer.roomwordsample.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zcaxer.roomwordsample.data.Building
import com.zcaxer.roomwordsample.data.BuildingRepository
import kotlinx.coroutines.launch

class BuildingViewModel(val buildingRepository: BuildingRepository) : ViewModel() {

    val buildings = buildingRepository.buildings

    fun setLevel(buildingId: Int, level: Int) {
        viewModelScope.launch {
            val b= buildings.value!![buildingId]
            b.level = level
            b.initBuilding()
            buildingRepository.updateBuilding(buildings.value!![buildingId])

        }
    }

    fun updateBuilding(buildingId: Int) {
        viewModelScope.launch {
            buildingRepository.updateBuilding(buildings.value!![buildingId])
        }
    }

    fun setBuildingWorkers(buildingId: Int, workers: Int) {
        viewModelScope.launch {
            buildingRepository.updateBuildingWorkers(buildingId, workers)
        }
    }

    fun setBuildingReserve(buildingId: Int, reserve: Int) {
        viewModelScope.launch {
            buildingRepository.updateBuildingReserve(buildingId, reserve)
        }
    }

}


